every
, some
Array#every
Array#every
[].every(callback);
callback(item [, index [,arr]])
Boolean
true
если все если все элементы массива удовлетворяют критериям callback()
false
если какой-либо из методов не удовлетворяет условию переданному в callback()
Array#every
In [2]:
function isOdd(number) {
return !!(number % 2);
}
console.log([1, 2, 3, 4].every(isOdd));
console.log([1, 3, 5, 7].every(isOdd));
Out[2]:
In [4]:
function isGreaterThan18(number) {
return number > 18;
}
console.log([22, 23].every(isGreaterThan18)); //true
console.log([19, 18].every(isGreaterThan18)); //false
Out[4]:
Array#some
Array#some
[].some(callback);
callback(item [, index [,arr]])
Boolean
true
если какой-либо удовлетворяет критериям указанным в callback()
false
если не один из элементов не удовлетворяет усвлоиям callback()
Array#some
In [5]:
function isOdd(number) {
return !!(number % 2);
}
console.log([1, 2, 3, 4].some(isOdd)); //true
console.log([1, 3, 5, 7].some(isOdd)); //true
console.log([2, 4, 6, 8].some(isOdd)); //false
Out[5]:
In [6]:
function isGreaterThan18(number) {
return number > 18;
}
console.log([22, 23].some(isGreaterThan18)); //true
console.log([19, 18].some(isGreaterThan18)); //true
console.log([17, 18].some(isGreaterThan18)); //false
Out[6]:
map
, reduce
, filter
Array#filter
Array#filter
[].filter(callback);
callback(item [, index [, arr]])
Array
callback()
In [7]:
function isOdd(number) {
return !!(number % 2);
}
console.log([1, 2, 3, 4].filter(isOdd)); // [1, 3]
console.log([1, 3, 5, 7].filter(isOdd)); // [1, 3, 5, 7]
console.log([2, 4, 6, 8].filter(isOdd)); // []
Out[7]:
In [9]:
function InRange(min, max) {
return function(item) { return min <= item && item <= max; };
}
var numbers = [2, 3, 4, 5, 6, 7, 8];
console.log(numbers.filter(InRange(4, 7))); // [4, 5, 6, 7]
console.log(numbers.filter(InRange(2, 4))); // [2, 3, 4]
Out[9]:
Array#reduce
Array#reduce
[].reduce(callback, initial);
callback(accumulator, item [, index [, arr]])
Object
callback()
In [10]:
var sum = [1, 2, 3, 4].reduce(function(sum, number) {
return sum + number;
}, 0);
var product = [1, 2, 3, 4].reduce(function(sum, number) {
return sum * number;
}, 1);
console.log(sum); //10
console.log(product); //24
Out[10]:
Array#map
Array#map
[].map(callback);
callback(item [, index [, arr]])
Object
callback()
Array#map
In [11]:
var squares = [1, 2, 3, 4, 5].map(function(number) {
return number * number;
});
console.log(squares); //prints [ 1, 4, 9, 16, 25 ]
Out[11]:
In [12]:
var lines = ['1 2 3',
'4 5 6'];
var matrix = lines.map(function(line) {
return line.split(' ')
.map(Number);
}); // [[1, 2, 3],
console.dir(matrix); // [4, 5, 6]]
Out[12]:
Array#forEach
Array#forEach
[].forEach(callback);
callback(item [, index [, arr]])
undefined
Array#forEach
In [14]:
var numbers = ['One', 'Two', 'Three', 'Four', 'Five'];
numbers.forEach(function(item, index) {
console.log('Item at ' + index + ' has value ' + item);
});
Out[14]:
In [31]:
function createPerson(name, age) {
return { name: name,
age: age,
introduce: function () {
console.log('Привет, мое имя ' + this.name);
}
};
};
var people = [createPerson('Петя', 13),
createPerson('Вася', 18),
createPerson('Вова', 21)];
people.forEach(function(person) {
person.introduce();
});
Out[31]:
find
, findIndex
Array#find
Array#find
[].find(callback);
callback(item [, index [, arr]])
Object
or undefined
callback()
undefined
Array#find
In [28]:
var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
numbers.find(function(item) {
return !!(item % 2) && item > 5;
}); //prints 7
Out[28]:
In [27]:
var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
numbers.find(function(item, index) {
return index > 3 && !!(item % 2);
}); //prints 5
Out[27]:
Array#findIndex
Array#findIndex
[].findIndex(callback);
callback(item [, index [, arr]])
Number
or -1
callback
-1
In [26]:
var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
numbers.findIndex(function(item) {
return !!(item % 2) && item > 5;
}); //prints 6(element 7)
Out[26]:
In [25]:
var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
numbers.findIndex(function(item, index) {
return index > 3 && !!(item % 2);
}); //prints 4(element 5)
Out[25]:
In [24]:
var numbers = [5, 1, 2, 4, 6];
numbers.sort(function(x, y) {
return y - x;
});
Out[24]:
In [47]:
var people = [createPerson('Семен', 13), createPerson('Вася', 18), createPerson('Жора', 18)];
people.sort(function(p1, p2) {
return p1.name > p2.name;
});
console.log(people);
Out[47]:
In [48]:
var count = 15,
arr = Array.from({length: count})
.fill(1);
console.log(arr);
Out[48]:
In [49]:
var count = 5,
arr = Array.from({length: count}) // [ [ 1, 2, 3, 4, 5 ],
.fill([1, 2, 3, 4, 5]); // [ 1, 2, 3, 4, 5 ],
// [ 1, 2, 3, 4, 5 ],
arr.fill([1, 2, 3, 4, 5]); // [ 1, 2, 3, 4, 5 ],
console.log(arr); // [ 1, 2, 3, 4, 5 ] ]
Out[49]:
In [97]:
var n = 10;
function getRandomDigit() { return (Math.random() * 10) | 0; }
function isOdd(item) { return !!(item % 2); }
function digitToDigitName(digit) {
return ['Ноль', 'Один','Два','Три','Четыре','Пять','Шесть','Семь','Восемь', 'Девять'][digit];
}
var digitNames = Array.from({length: n})
.fill(0)
.map(getRandomDigit)
.filter(isOdd)
.map(digitToDigitName);
console.log(digitNames);
Out[97]:
In [94]:
var digits = Array(10)
var digitNames = digits.fill(0)
.map(x => Math.random()*10 | 0)
.filter(x=> !!(x%2))
.map(x=> ['Ноль', 'Один','Два','Три','Четыре','Пять','Шесть','Семь','Восемь', 'Девять'][x]);
console.log(digitNames);
Out[94]:
In [ ]: